package dao

import (
	"github.com/gin-gonic/gin"
	"web_api_gin/dto"
	"web_api_gin/entity"
)

var SysFile = SysFileDao{}

type SysFileDao struct {
}

func (d *SysFileDao) GetList(ctx *gin.Context, searchParams *dto.SysFileGetListReq) ([]*entity.SysFile, int64, error) {
	var list []*entity.SysFile
	var total int64
	tx := DB(ctx).Model(&entity.SysFile{})
	if searchParams.Keyword != "" {
		tx = tx.Where("origin_name like ?", "%"+searchParams.Keyword+"%")
	}
	if searchParams.FileType != "" {
		tx = tx.Where("file_type = ?", searchParams.FileType)
	}
	if len(searchParams.TimeArea) > 0 {
		//startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", searchParams.TimeArea[0], time.Local)
		//endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", searchParams.TimeArea[len(searchParams.TimeArea)-1], time.Local)
		tx = tx.Where("created_at BETWEEN ? AND ?", searchParams.TimeArea[0], searchParams.TimeArea[len(searchParams.TimeArea)-1])
	}
	err := tx.Count(&total).Order("file_id DESC").Limit(int(searchParams.PageSize)).Offset(int((searchParams.PageNo - 1) * searchParams.PageSize)).Find(&list).Error
	for _, file := range list {
		file.CreatedAtStr = file.CreatedAt.Format("2006-01-02 15:04:05")
	}
	return list, total, err
}
